package com.hnkywl.sts.dao.report;

import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.sql.Date;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-8-6
 * Time: 下午7:34
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class BgjhdxlDao extends SimpleHibernateTemplate{
    public Map<String, Object> bgjhdxlList(Integer nf,Integer yf,String jhlx,Long jhdw,Long gyz,String wzbm,String jhsm,Integer pageNo, Integer pageSize) {
        if (nf == null)
            nf = -1;
        if (yf == null)
            yf = -1;
        if (wzbm == null || "".equals(wzbm))
            wzbm = "-1";
        if (jhlx == null || "".equals(jhlx))
            jhlx = "-1";
        if (jhsm == null || "".equals(jhsm))
            jhsm = "-1";
        if (jhdw == null)
            jhdw = -1L;
        if (gyz == null)
            gyz = -1L;
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        Integer total = 0;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call RPT_BG_JHDXL(?,?,?,?,?,?,?,?,?,?,?,?,?)}");
            call.setInt(1, nf);
            call.setInt(2, yf);
            call.setString(3, jhlx);
            call.setLong(4, jhdw);
            call.setLong(5, gyz);
            call.setString(6, wzbm);
            call.setString(7, jhsm);
            call.setInt(8, (pageNo - 1) * pageSize);
            call.setInt(9, pageNo * pageSize);
            call.registerOutParameter(10, Types.NUMERIC);
            call.registerOutParameter(11, oracle.jdbc.OracleTypes.CURSOR);
            call.registerOutParameter(12, OracleTypes.VARCHAR);
            call.registerOutParameter(13, OracleTypes.VARCHAR);
            call.execute();
            //====================================
            total = call.getInt(10);
            rs = ((OracleCallableStatement) call).getCursor(11);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map> rows = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                rows.add(r);
            }
            result.put("total", total);
            result.put("rows", rows);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }
}
